﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace Zero.UControl.Base.Helper
{
    public class HotKeyHelper
    {
        [DllImport("user32.dll")]
        public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

        [DllImport("user32.dll")]
        public static extern bool UnregisterHotKey(IntPtr hWnd, int id);

        // 修饰键常量
        public const uint MOD_NONE = 0x0000;
        public const uint MOD_ALT = 0x0001;
        public const uint MOD_CTRL = 0x0002;
        public const uint MOD_SHIFT = 0x0004;
        public const uint MOD_WIN = 0x0008;

        // 键码
        public const uint VK_ESC = 0x70;
        public const uint VK_F1 = 0x71;
        public const uint VK_F2 = 0x72;
        public const uint VK_F3 = 0x73;
        public const uint VK_S = 0x74;

        // 热键 ID
        public const int HOTKEY_ID_ESC = 9000;
        public const int HOTKEY_ID_F1 = 9001;
        public const int HOTKEY_ID_F2 = 9002;
        public const int HOTKEY_ID_F3 = 9003;
        public const int HOTKEY_ID_S = 9004;

        public static void Register(IntPtr handle)
        {
            RegisterHotKey(handle, HOTKEY_ID_S, MOD_CTRL | MOD_ALT, VK_S);     // Ctrl+Alt+S
            RegisterHotKey(handle, HOTKEY_ID_ESC, MOD_NONE, VK_ESC);             // ESC
            RegisterHotKey(handle, HOTKEY_ID_F1, MOD_NONE, VK_F1);             // F1
            RegisterHotKey(handle, HOTKEY_ID_F2, MOD_NONE, VK_F2);             // F2
            RegisterHotKey(handle, HOTKEY_ID_F3, MOD_NONE, VK_F3);             // F3
        }

        public static void Unregister(IntPtr handle)
        {
            UnregisterHotKey(handle, HOTKEY_ID_ESC);
            UnregisterHotKey(handle, HOTKEY_ID_F1);
            UnregisterHotKey(handle, HOTKEY_ID_F2);
            UnregisterHotKey(handle, HOTKEY_ID_F3);
            UnregisterHotKey(handle, HOTKEY_ID_S);

        }
    }

}